import 'package:boss/models/store_area.dart';
import 'package:boss/provider/table_provider.dart';

import 'package:boss/ui/page/tableManagement/page/areaManagement/page/areaDetail/provider/area_detail_provider.dart';
import 'package:boss/ui/page/tableManagement/page/areaManagement/page/areaDetail/widget/area_detail_browser.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/back_confirmation.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

@RouterConfig(AreaDetailPage.pageName)
class AreaDetailPage extends StatefulWidget {
  static const pageName = '/areaDetailPage';

  @Autowired()
  final StoreArea storeArea;

  const AreaDetailPage({Key key, this.storeArea}) : super(key: key);

  @override
  _AreaDetailPageState createState() => _AreaDetailPageState('新增区域');
}

class _AreaDetailPageState extends BaseState<AreaDetailPage> {
  _AreaDetailPageState(String pageName) : super(pageName);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (ctx) => AreaDetailProvider(storeArea: widget.storeArea),
      child: BackConfirmation(
        backConfirmationText: '区域还未完成确认返回么？',
        child: Scaffold(
          appBar: CenterTitleAppBar(
            title: '新增区域',
            backConfirmationText: '区域还未完成确认返回么？',
            actions: <Widget>[
              Consumer2<AreaDetailProvider, TableProvider>(
                builder: (context, areaDetailProvider, tableProvider, _) {
                  return areaDetailProvider.isUpdate
                      ? LabelButton(
                          title: '删除',
                          padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                          onTap: () async {
                            showAlert(
                              context,
                              content: '确定要删除该区域吗？',
                              onConfirm: (ctx) async {
                                Navigator.pop(ctx);
                                var result = await tableProvider.deleteStoreArea(areaDetailProvider.storeArea);
                                if (result) Navigator.pop(context);
                              },
                              onCancel: (context) {
                                Navigator.pop(context);
                              },
                            );
                          },
                        )
                      : Container();
                },
              ),
            ],
          ),
          body: AreaDetailBrowser(),
        ),
      ),
    );
  }
}
